package com.lwj.liwanjia.view.fragment.search;


import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.EditText;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.lwj.liwanjia.R;
import com.lwj.liwanjia.adapter.SearchListAdapter;
import com.lwj.liwanjia.base.BaseFragment;
import com.lwj.liwanjia.bean.BaobeiBean;
import com.lwj.liwanjia.bean.LitepalBean;
import com.lwj.liwanjia.impl.BaobeiView;
import com.lwj.liwanjia.presenter.BaobeiPresenter;
import com.lwj.liwanjia.utils.SpfUtils;
import com.lwj.liwanjia.view.activity.search.SearchActivity;
import com.lwj.liwanjia.view.activity.supermarket.ShangpinDetailActivity;
import com.superrecycleview.superlibrary.adapter.SuperBaseAdapter;
import com.superrecycleview.superlibrary.recycleview.SuperRecyclerView;

import org.litepal.crud.DataSupport;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.Unbinder;

/**
 * 模糊搜索(宝贝)
 */
public class SearchListFragment extends BaseFragment implements BaobeiView {

    @BindView(R.id.search_list)
    SuperRecyclerView searchList;
    @BindView(R.id.rl_nosearch)
    RelativeLayout noSearch;
    @BindView(R.id.progress)
    ProgressBar progressBar;

    private SearchListAdapter searchListAdapter;
    private List<BaobeiBean.DataBean> list = new ArrayList<>();
    private SearchActivity activity;
    private EditText search;
    private BaobeiPresenter baobeiPresenter;//搜索宝贝
    private String token;

    @Override
    protected void initView(View view) {
        LinearLayoutManager manager = new LinearLayoutManager(getActivity());
        manager.setOrientation(LinearLayoutManager.VERTICAL);
        searchList.setLayoutManager(manager);
        searchList.setRefreshEnabled(false);
        searchList.setLoadMoreEnabled(false);
        activity = (SearchActivity) getActivity();
        search = activity.getSearch();
        searchListAdapter = new SearchListAdapter(getActivity(), list);
        searchList.setAdapter(searchListAdapter);
        token = SpfUtils.getSpfUtils(getActivity()).getToken();
        baobeiPresenter = new BaobeiPresenter(this);
        clickLsitener();
        search.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                baobeiPresenter.getBaobei(token, s + "");
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

    //搜索后保存搜索文本
    private void save() {
        LitepalBean litepal = new LitepalBean();
        String historyName = search.getText().toString();
        litepal.setHistoryName(historyName);
        //判断数据库中是否已存在该数据，没有就存储
        List<LitepalBean> beanList = DataSupport.where("historyName=?", historyName).find(LitepalBean.class);
        if (beanList.size() == 0) {
            //更新数据库
            litepal.save();
        }
    }

    //点击事件
    private void clickLsitener() {
        searchListAdapter.setOnItemClickListener(new SuperBaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, Object item, int position) {
                Intent intent = new Intent(getActivity(), ShangpinDetailActivity.class);
                Bundle bundle = new Bundle();
                bundle.putString("shopGoodsCode", list.get(position).getCommodityCode());
                bundle.putString("commodityType", list.get(position).getCommodityType());
                intent.putExtras(bundle);
                startActivity(intent);
                //看详情时保存搜索的内容
                save();
            }
        });
    }

    @Override
    protected void initData() {
        baobeiPresenter.getBaobei(token, search.getText().toString());
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_search_list;
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        if (!hidden) {
            initData();
        }
    }

    @Override
    public void showProgress() {
        progressBar.setVisibility(View.VISIBLE);
    }

    @Override
    public void hideProgress() {
        progressBar.setVisibility(View.GONE);
    }

    @Override
    public void showError(Exception e) {
        Toast.makeText(activity, "数据加载失败", Toast.LENGTH_SHORT).show();
    }

    //搜索出来的商品
    @Override
    public void getBaobei(BaobeiBean baobeiBean) {
        if (baobeiBean.getState() == 0) {
            Toast.makeText(activity, "接口有误", Toast.LENGTH_SHORT).show();
        } else if (baobeiBean.getState() == 1) {
            list.clear();
            list.addAll(baobeiBean.getData());
            if (list.size() == 0) {
                noSearch.setVisibility(View.VISIBLE);
                searchList.setVisibility(View.GONE);
            } else {
                noSearch.setVisibility(View.GONE);
                searchList.setVisibility(View.VISIBLE);
                searchListAdapter.notifyDataSetChanged();
            }
        }
    }
}
